package com.wisdytech.linkdcs.factory.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.factory.model.Factory;
import com.wisdytech.linkdcs.factory.service.TmFactoryService;
import com.wisdytech.linkdcs.factory.vo.FactoryVO;
import com.wisdytech.linkdcs.system.model.SysUser;
import com.wisdytech.linkdcs.system.vo.TreeviewNodeVO;
/**
 * 
 * <p>
 * ClassName: FactoryAction
 * </p>
 * <p>
 * Description: 厂区管理页面的action层
 * </p>
 * <p>
 * Author: chenbin
 * </p>
 * <p>
 * Date: 2018-5-3
 * </p>
 */
@Controller
@RequestMapping("/factory/factoryArea")
public class FactoryAction {
    /**
     * 注入厂区tmFactoryService
     */
    @Autowired
    @Qualifier("tmFactoryService")
    private TmFactoryService tmFactoryService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "factoryModel/factoryArea";
    }
    /**
     * 分页条件查询厂区管理列表信息
     * @param factoryCode 工序编码
     * @param factoryName 工序名称
     * @param filter 分页参数
     * @return 厂区管理列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listFactoryByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<FactoryVO> listFactoryByFilter(String factoryCode, String factoryName, Filter filter) throws IOException{
    	PageInfo<Factory> factorys =this.tmFactoryService.listFactoryByFilter(factoryCode, factoryName, filter);
        //转换为前台结果集
        List<Factory> factoryList=factorys.getList();
        List<FactoryVO> factoryVOList=new ArrayList<FactoryVO>();
        for(Factory factory:factoryList){
        	factoryVOList.add(DataTransformUtil.changeFactoryVO(factory));
        }
        return new StandardTableResult<FactoryVO>(factorys.getTotal(),factoryVOList);
    }
    
    /**
     * 保存厂区信息
     * @param factory 厂区对象
     * @param session 
     * @return 保存结果集
     */
    @RequestMapping(value = "/saveFactory/save.mvc")
    @ResponseBody
    public StandardResult saveOperation(Factory factory, HttpSession session) {
        SysUser sysUser = BaseUtil.getCurrentUser();
        String loginName = sysUser.getUsername();
        if (StringUtils.isBlank(factory.getPkId())) {
            tmFactoryService.addFactory(factory, loginName);
        }else {
            tmFactoryService.updateFactory(factory, loginName);
        }
        return  StandardResult.getSuccessResult();
    }
    
    /**
     * 通过id批量删除厂区信息
     * @param id 主键id
     * @return 删除结果集
     */
    @RequestMapping(value = "/delFactoryById/del.mvc")
    @ResponseBody
    public StandardResult delFactoryById(String pkId) {
    	tmFactoryService.delFactoryById(pkId);
        return StandardResult.getSuccessResult();
    }
    
    /**
     * 通过id获取当前厂区信息
     * @param id 主键id
     * @return 前台厂区显示对象
     */
    @RequestMapping(value = "/getFactoryById/get.mvc")
    @ResponseBody
    public FactoryVO getSysOperationById(String pkId) {
    	Factory factory = tmFactoryService.getFactoryById(pkId);
        FactoryVO factoryVO = DataTransformUtil.changeFactoryVO(factory);
        return factoryVO;
    }
    
    /**
     * <p>Description:查询厂区信息列表</p> 
     * @return 厂区信息列表集合
     * @throws IOException 
     */
    @RequestMapping(value = "/listAllFactory/get.mvc")
    @ResponseBody
    public List<Factory> listAllFactory(@ModelAttribute("search") String search) throws IOException{
        return tmFactoryService.listAllFactory(search);
    }
    
    @RequestMapping(value = "/listFactoryTree/get.mvc")
    @ResponseBody
    public StandardResult listFactoryTree() {
    	List<TreeviewNodeVO>  data=tmFactoryService.getFactoryTree();
    	return StandardResult.getSuccessResult(data);
    }
}
